package action;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import action.imps.IDataService;

import com.opensymphony.xwork2.ActionSupport;

import dao.DataServiceDAOImpl;

public class DataService implements IDataService {
	/*
	 * (non-Javadoc)
	 * 
	 * @see action.Implements.IDataService#VSFileReadByID()
	 */
	@Override
	public void VSFileReadByID() throws Exception {
		// TODO Auto-generated method stub
		// 1 获取前台请求参数
		HttpServletRequest request = ServletActionContext.getRequest();
		String jsonData = request.getParameter("jsonData");
		JSONObject jsonObject = JSONObject.fromObject(jsonData);
		String urlname = (String) jsonObject.get("url");
		// 2 整合pageXml路径
		// /获取strust2内存中已经加载的properties属性文件
		ActionSupport actionSupport = new ActionSupport();
		String baseUrl = actionSupport.getText("fileDisk.baseUrl");
		String PageManagerName = actionSupport.getText("fileDisk.PageManager");
		String allPath = baseUrl + "/" + PageManagerName + "/" + urlname
				+ ".xml";
		// 3 获取pageXml内容
		DataServiceDAOImpl VSFileDAO = new DataServiceDAOImpl();
		String jsonText = VSFileDAO.getPageXml(allPath);
		String jsonResult = "{\"result\":\"true\",\"message\":\"执行成功\",\"data\":"
				+ jsonText + "}";
		// 4 返回“返回值”
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=utf-8");
		PrintWriter printWriter = response.getWriter();
		printWriter.print(jsonResult);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see action.Implements.IDataService#DSReadData()
	 */
	@Override
	public void DSReadData() throws Exception {
		// TODO Auto-generated method stub
		HttpServletRequest request = ServletActionContext.getRequest();
		String jsonData = request.getParameter("jsonData");
		JSONObject jsonObject = JSONObject.fromObject(jsonData);
		String datasetID = (String) jsonObject.get("datasetID");
		// String param = (String) jsonObject.get("param");

		DataServiceDAOImpl dao = new DataServiceDAOImpl();
		List list = dao.getData(datasetID);
		JSONArray ary = JSONArray.fromObject(list);
		String data = ary.toString();
		String jsonResult = "{\"result\":\"true\",\"message\":\"执行成功\",\"Data\":"
				+ data + "}";
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=utf-8");
		PrintWriter pw = response.getWriter();
		pw.write(jsonResult);
	}

}
